package com.codebo.homiematch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebo.homiematch.common.ErrorCode;
import com.codebo.homiematch.constant.UserConstant;
import com.codebo.homiematch.exception.BusinessException;
import com.codebo.homiematch.exception.ThrowUtils;
import com.codebo.homiematch.mapper.UserMapper;
import com.codebo.homiematch.model.entity.User;
import com.codebo.homiematch.model.vo.UserVO;
import com.codebo.homiematch.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * @author 25912
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-07-17 11:56:36
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService
{

    @Override
    public UserVO login(String username, String password, HttpServletRequest httpServletRequest)
    {
        //验证参数
        ThrowUtils.throwIf(StrUtil.hasBlank(username, password), ErrorCode.PARAMS_ERROR, "参数不能为空");
        ThrowUtils.throwIf(username.length() < 4, ErrorCode.PARAMS_ERROR, "账号长度太短");
        ThrowUtils.throwIf(password.length() < 4, ErrorCode.PARAMS_ERROR, "密码长度太短");

        //构建查询条件
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", username);
        qw.eq("password", password);
        User user = baseMapper.selectOne(qw);

        //验证用户
        ThrowUtils.throwIf(user == null, ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
        ThrowUtils.throwIf(user.getStatus() == 0, ErrorCode.NO_AUTH_ERROR, "用户被禁用");

        //将用户信息存入session
        httpServletRequest.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, user);
        return getUserVO(user);
    }

    @Override
    public Long register(String username, String password, String checkPassword, List<String> tags, String nickname, int longitude, int latitude)
    {
        //验证参数
        ThrowUtils.throwIf(StrUtil.hasBlank(username, password), ErrorCode.PARAMS_ERROR, "参数不能为空");
        ThrowUtils.throwIf(username.length() < 4, ErrorCode.PARAMS_ERROR, "账号长度太短");
        ThrowUtils.throwIf(!password.equals(checkPassword), ErrorCode.PARAMS_ERROR, "密码不一致");
        ThrowUtils.throwIf(password.length() < 4, ErrorCode.PARAMS_ERROR, "密码长度太短");
        ThrowUtils.throwIf(tags.isEmpty(), ErrorCode.PARAMS_ERROR, "请至少选择一个标签");

        //构建查询条件
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", username);
        User user = baseMapper.selectOne(qw);

        //验证用户
        ThrowUtils.throwIf(user != null, ErrorCode.PARAMS_ERROR, "账号已存在");

        //插入数据库
        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setTags(JSONUtil.toJsonStr(tags));
        user.setNickname(nickname);
        user.setLongitude(longitude);
        user.setLatitude(latitude);
        boolean save = save(user);
        if (!save)
        {
            log.error("数据库错误，用户注册时出错");
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "注册失败");
        }


        return user.getId();
    }

    @Override
    public List<UserVO> loveType()
    {
        return Collections.emptyList();
    }

    @Override
    public UserVO getUserVO(User user)
    {
        UserVO userVO = new UserVO();
        userVO.setTags(JSONUtil.parseArray(user.getTags()).toList(String.class));
        BeanUtil.copyProperties(user, userVO);
        return userVO;
    }

    @Override
    public User getLoginUser(HttpServletRequest httpServletRequest)
    {
        Object object = httpServletRequest.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        ThrowUtils.throwIf(object == null, ErrorCode.NOT_LOGIN_ERROR);
        return (User) object;
    }
}




